<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

require_once('includes/functions/module_functions.php');
class flat
{
	var $code, $enabled;
    	function flat() 
	{
		$this->module_key = 'MODULE_SHIPPING_FLAT';
		$this->code = 'flat';
		$this->enabled = ((MODULE_SHIPPING_FLAT_STATUS == 'true') ? true : false);
		$this->sort_order = MODULE_SHIPPING_FLAT_SORT_ORDER;
	}

    	function quote($params, &$checkout_component)
	{
		if (!$this->enabled) return false;

		$shipping_service = $params['shipping_service'];
		if ($shipping_service == null || strlen($shipping_service) == 0)
			$shipping_service = null;

		$shipping_weight = (double)$params['shipping_weight'];
		//Make sure that a weight exists.
		if ($shipping_weight == 0) $shipping_weight = .1;
		
		$shipping_num_boxes = $params['shipping_num_boxes'];	
	
		
		//confirm Zone
		$check_flag = check_zone(MODULE_SHIPPING_FLAT_ZONE, $checkout_component->shipTo['country_id'], $checkout_component->shipTo['zone_id']);
		if ($check_flag == false) return false;

		$quotes['code'] = $this->code;
		$quotes['tax_class'] = MODULE_SHIPPING_FLAT_TAX_CLASS;

		$rates = array();
		$rates[] = array('service' => 'flat', 'postage' => (double)MODULE_SHIPPING_FLAT_COST);

		//Calculate Handling
		$quotes['handling'] = (double)MODULE_SHIPPING_FLAT_HANDLING;
		$quotes['rates'] = $rates;

		return $quotes;	
	}

	function check() {
		if (!isset($this->_check)) {
			$this->_check = check_module($this->module_key);
		}
		return $this->_check;
	}

	function install() {
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, configuration_values, date_added) values ('Enable Flat Shipping', 'MODULE_SHIPPING_FLAT_STATUS', 'true', 'Do you want to offer flat rate shipping?', '6', '0','true, false', now())");
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Shipping Cost', 'MODULE_SHIPPING_FLAT_COST', '5.00', 'The shipping cost for all orders using this shipping method.', '6', '0', now())");
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, configuration_values, date_added) values ('Tax Class', 'MODULE_SHIPPING_FLAT_TAX_CLASS', '0', 'Use the following tax class on the shipping fee.', '6', '0', 'tax_class_list()', now())");
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, configuration_values, date_added) values ('Shipping Zone', 'MODULE_SHIPPING_FLAT_ZONE', '0', 'If a zone is selected, only enable this shipping method for that zone.', '6', '0', 'geo_zone_list()', now())");
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_SHIPPING_FLAT_SORT_ORDER', '0', 'Sort order of display.', '6', '0', now())");
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Handling Fee', 'MODULE_SHIPPING_FLAT_HANDLING', '0', 'Handling fee for this shipping method.', '6', '0', now())");
	}

	function remove() {
		remove_module($this->module_key);
	}
}
?>
